/**
 * 
 */
package org.seginfo.httpfuzzer.header.impl;

import java.util.Random;

import org.seginfo.httpfuzzer.header.AttackStrategy;
import org.seginfo.httpfuzzer.header.HeaderPropertyImpl;

/**
 * @author Administrador
 *
 */
public class TE extends HeaderPropertyImpl {
	
	private static final String NAME = "TE";
	private static final String VALID = "trailers, deflate;q=0.5";
	//private static final String WILDCARD = "*/*";
	private static final int BAD_STRING_MIN_LENGTH = 16;
	private static final int BAD_STRING_LENGTH = 256*4;

	private Random r;
	/**
	 * @param attackStrategy
	 */
	public TE(AttackStrategy attackStrategy) {
		super(attackStrategy);
		r = new Random(9);
		this.value = this.generateValue();
	}

	/**
	 * @see org.seginfo.httpfuzzer.header.HeaderPropertyImpl#getName()
	 */
	public String getName() {
		return NAME;
	}

	/**
	 * @return
	 */
	private String generateValue() {
		
		String rv;

		double p = r.nextDouble();

		if (p < 0.002) {
			rv = VALID;
		} else if (p < 0.4) {
			rv = "trailers, "+attackStrategy.getStringAttack(BAD_STRING_MIN_LENGTH, BAD_STRING_LENGTH);
		} else if (p < 0.7) {
			rv = "trailers";

			int size = (int) (r.nextDouble() * 10) + 1;
			for (int i = 0; i < size; i++) {
				if (!rv.equals("")) {
					rv += ',';
				}
				rv += attackStrategy.getStringAttack(BAD_STRING_MIN_LENGTH, BAD_STRING_LENGTH);
				if (i % 2 == 0) {
					rv += ';' + QUALITY + '=' +
					attackStrategy.getFloatAttack();
				}
			}
		} else {
			rv = attackStrategy.getStringAttack(BAD_STRING_MIN_LENGTH, BAD_STRING_LENGTH);
		}

		return rv;
	}
}
